package exercise;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * ��ҵ1��
   �������50����������(���� 20-25)��50��Ů������(18-22) ��ʽ:
��ط��-��-23
��ϣ��-Ů-18
��˼��-Ů-22
������-��-25
��ϣ��-��-22
����-Ů-22
 */
public class Exercise7 {
	public static void main(String[] args) throws Exception {
		String familyNameNet="https://www.jimingbao.com/90981.html";
		String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
	    String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";
        String familyNameStr = webCrawler(familyNameNet);
        String boyNameStr = webCrawler(boyNameNet);
        String girlNameStr = webCrawler(girlNameNet);
	      
        ArrayList<String> familyNameTempList = getData(familyNameStr,"(.、){2}");
        ArrayList<String> boyNameTempList = getData(boyNameStr,"(..、){8}");
        ArrayList<String> girlNameTempList = getData(girlNameStr,"(.. ){4}");
        
        System.out.println(familyNameTempList);
        System.out.println(boyNameTempList);
        System.out.println(girlNameTempList);
        
        ArrayList<String> familyNameList = new ArrayList<>();
        
        for (String xing : familyNameTempList) {
			String[] split = xing.split("、");
			for(int i=0;i<split.length;i++){
				familyNameList.add(split[i]);
			}
		}
      System.out.println(familyNameList);
      
      ArrayList<String> boyNameList = new ArrayList<>();
		for (String boyming : boyNameTempList) {
			String[] split = boyming.split("、");
			for(int i=0;i<split.length;i++){
				boyNameList.add(split[i]);
			}
		}
		System.out.println(boyNameList);
        
	  
        ArrayList<String> girlNameList = new ArrayList<>();
        for (String str : girlNameTempList) {
            String[] arr = str.split(" ");
            for (int i = 0; i < arr.length; i++) {
                girlNameList.add(arr[i]);
            }
        }
        System.out.println(girlNameList);
	  
        ArrayList<String> list = getInfos(familyNameList, boyNameList, girlNameList, 50, 50);
        Collections.shuffle(list);


        BufferedWriter bw = new BufferedWriter(new FileWriter("names.txt"));
        for (String str : list) {
            bw.write(str);
            bw.newLine();
        }
        bw.close();

	}

	private static ArrayList<String> getData(String str, String regex) {
		ArrayList<String> list=new ArrayList<>();
		Pattern pattern=Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		while(matcher.find()){
			String group = matcher.group();
			list.add(group);
		}
		return list;
	}
	
	 public static ArrayList<String> getInfos(ArrayList<String> familyNameList,ArrayList<String> boyNameList,ArrayList<String> girlNameList, int boyCount,int girlCount){
	        HashSet<String> boyhs = new HashSet<>();
	        while (true){
	            if(boyhs.size() == boyCount){
	                break;
	            }
	            Collections.shuffle(familyNameList);
	            Collections.shuffle(boyNameList);
	            boyhs.add(familyNameList.get(0) + boyNameList.get(0));
	        }
	        HashSet<String> girlhs = new HashSet<>();
	        while (true){
	            if(girlhs.size() == girlCount){
	                break;
	            }
	            //随机
	            Collections.shuffle(familyNameList);
	            Collections.shuffle(girlNameList);
	            girlhs.add(familyNameList.get(0) + girlNameList.get(0));
	        }
	        ArrayList<String> list = new ArrayList<>();
	        Random r = new Random();
	        for (String boyName : boyhs) {
	            int age = r.nextInt(6) + 20;
	            list.add(boyName + "-男-" + age);
	        }
	        for (String girlName : girlhs) {
	            int age = r.nextInt(5) + 18;
	            list.add(girlName + "-女-" + age);
	        }
	        return list;
	    }

	
	private static String webCrawler(String net) throws Exception {
		StringBuilder sb = new StringBuilder();
		URL url = new URL(net);
		URLConnection connection = url.openConnection();
		InputStreamReader isr = new InputStreamReader(connection.getInputStream());
		int ch;
		while((ch=isr.read())!=-1){
			sb.append((char)ch);
		}
		return sb.toString();
	}
}
